package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.R;
import com.example.pojo.entity.WmNews;
import com.example.pojo.req.CommentReq;
import com.example.service.NewsService;
import com.example.service.impl.NewsServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.example.utils.JsonChangeObject.JsonObject;


@WebServlet("/news")
public class NewsServlet extends BaseServlet {

    NewsService newsService = new NewsServiceImpl();

    /**
     * 获取文章列表数据
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    public void getNewsList(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        req.setCharacterEncoding("utf-8");
        String channelId = req.getParameter("channelId");
        String title = req.getParameter("title");
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer size = Integer.valueOf(req.getParameter("size"));
        R newsList = newsService.getNewsList(channelId, title, page, size);
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(newsList));
    }

    /**
     * 获取文章详情数据
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    public void getNewsDetail(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        String newsId = req.getParameter("newsId");
        R newsList = newsService.getNewsDetail(newsId);
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(newsList));
    }

    /**
     * 获取热榜数据标题
     */
    public void getHotTitle(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        R newsList = newsService.getNewsHotTitle();
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(newsList));
    }

    /**
     * 获取热点数据
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    public void getHotNewsList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        Integer size = Integer.valueOf(req.getParameter("size"));
        R addNews = newsService.getHotNewsList(size);
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(addNews));
    }


    /**
     * 获取个人作品
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    public void getPersonWork(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer size = Integer.valueOf(req.getParameter("size"));
        String title = req.getParameter("title");
        String token = req.getHeader("token");
        R addNews = newsService.getPersonWorkList(page, size, token,title);
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(addNews));
    }


    /**
     * 删除人作品
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    public void deleteWork(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        String newsId = req.getParameter("newsId");
        R success = newsService.deleteWork(newsId);
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(success));
    }

}
